package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.StrategyDetail;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.service.IStrategyCatalogService;
import cn.wolfcode.trip.base.service.IStrategyDetailService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 大攻略资源管理控制器
 */
@Controller
@RequestMapping("/strategyDetail")
public class StrategyDetailController {
    @Autowired
    IStrategyDetailService strategyDetailService;
    @Autowired
    IStrategyService strategyService;
    @Autowired
    IStrategyCatalogService strategyCatalogService;
    /**
     * 分页
     * @return
     */
    @RequestMapping("/list")
    public String strategyDetailQuery(@ModelAttribute("qo") QueryObject qo, Model model){
        //查询全部大攻略
        model.addAttribute("pageInfo",strategyDetailService.query(qo));
        model.addAttribute("strategies",strategyService.listAll());
        return "strategyDetail/list";
    }

    /**
     * 二级联动获取数据
     * @param strategyId
     * @return
     */
    @RequestMapping("/listByStrategyId")
    @ResponseBody
    public List listByStrategyId(Long strategyId){
       return strategyCatalogService.listAll(strategyId);
    }

    /**
     * 新增和编辑
     * @param strategyDetail
     * @param file
     * @return
     */
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(StrategyDetail strategyDetail, MultipartFile file){
        //上传前台传来的图片
        if (file!=null&&file.getSize()>0){
            String upload = UploadUtil.upload(file, UploadUtil.PATH + "/upload");
            //将新的封面设置给推荐
            strategyDetail.setCoverUrl(upload);
        }
        strategyDetailService.saveOrUpdate(strategyDetail);
        return new JSONResult();
    }
}
